<?php
/**
 * 院校网站接口类
 */
class schoolApi
{
    private $db;
    private $do_same;
    
    public function __construct()
    {
        global $db;
        $this->db = $db;
        $this->set_do_same();
    }
    
    public function set_do_same($flag = false)
    {
    	$this->do_same = $flag;
    }
    
    public function get_cat_contents($siteid,$catcode,$school)
    {
    	$str = constant('SCHOOL_CATCONTENT_API_' . strtoupper($school));
    	$apifile = sprintf($str,$catcode,$siteid);
    	$res = file_get_contents($apifile);
    	$res = json_decode($res);
    	if(sizeof($res) != 4)
    	{
    		return true;
    	}
		$contents = $res[3];
		
		$s_catcode = $this->get_s_code();
		if(!in_array($catcode, $s_catcode))
		{
			$res = $this->add_cat_content($contents,$siteid,$catcode,$school);
		}
		else 
		{
			$func = 'add_'.$catcode;
			$res = $this->$func($contents,$siteid,$school);
		}
    	return true;
    }
    
    /**
     * 东财在线中非新闻的内容类型
     *
     * @return array
     */
    public function get_s_code()
    {
    	$s_catcode = array('subject','announce','teacher','course','faq');
    	return $s_catcode;
    }
    
    /**
     * 获得专业
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_subject($params,$siteid,$school='')
    {
    	global $mconf;
    	$modules = get_moudule_info();
    	if(!isset($modules['college']))
    	{
    		return false;
    	}
    	$module_conf = $modules['college'];
    	$site_list = array();
    	
    	if(empty($siteid))
    	{
    		$this->db->prepare('select * from ' . DB_PRE . 'sites where ismaster=? and status=?');
    		$this->db->set_int(0);
    		$this->db->set_int(STATUS_NORMAL);
    		$res = $this->db->execute();
    	}
    	else 
    	{
    		$this->db->prepare('select * from ' . DB_PRE . 'sites where ismaster=? and status=? and id=?');
    		$this->db->set_int(0);
    		$this->db->set_int(STATUS_NORMAL);
    		$this->db->set_string($siteid);
    		$res = $this->db->execute();
    	}
    	foreach ($res as $v)
    	{
    		$tmp_mods = explode(',',$v['modules']);
    		if(in_array('college', $tmp_mods))
    		{
    			$site_list[] = $v['id'];
    		}
    	}
    	$res = true;
    	if(is_array($site_list))
    	{
    		foreach ($site_list as $siteid)
    		{
		    	if(is_array($params))
		    	{
		    		foreach ($params as $p)
		    		{
		    			$this->db->prepare('select *  from ' . DB_PRE . 'subject where subjectcode=? and siteid=? and school=? and level=?');
		    			$this->db->set_string($p[0]);
		    			$this->db->set_string($siteid);
		    			$this->db->set_string($school);
		    			$this->db->set_string($p[2]);
		    			$info = $this->db->execute(true);
		    			if(isset($info['subjectid']) && $info['status']!=STATUS_DEL && $this->do_same == false)
		    			{
		    				continue;
		    			}
		    			if(!isset($info['subjectid']))
		    			{
		    				$params = array(
		    					'subjectcode'	=>	$p[0],
		    					'siteid'		=>	$siteid,
		    					'school'		=>	$school,
		    					'level'			=>	$p[2],
		    					'model'			=>	$module_conf['content_model'],
		    					'subjectname'	=>	$p[1],
		    					'addtime'		=>	time(),
		    					'status'		=>	STATUS_WAIT,
		    					'listorder'		=>	0,
		    					'link'			=>	$p[3],
		    					'is_push'		=>	1
		    				);
		    				$res = $this->db->insert(DB_PRE . 'subject', $params);
		    				$uuid = $this->db->uuid();
		    				$_p = array(
								'catname'	=>	$params['subjectname'],
								'siteid'	=>	$params['siteid'],
								'modelid'	=>	$module_conf['content_model'],
								'workflowid'	=>	$module_conf['content_wf']
							);
							$mconf->set_conf($_p, $module_conf['name'],$uuid);
		    			}
		    			else 
		    			{
		    				$params = array(
		    					'subjectname'	=>	$p[1],
		    					'link'			=>	$p[3]
		    				);
		    				if($info['status'] == STATUS_DEL)
		    				{
		    					$params['status'] = STATUS_WAIT;
		    				}
		    				$res = $this->db->simple_update(DB_PRE . 'subject', $params, array('subjectid'=>$info['subjectid']));
		    				$_p = array(
								'catname'	=>	$params['subjectname'],
							);
							$mconf->set_conf($_p, $module_conf['name'],$info['subjectid']);
		    			}
		    		}
		    	}
    		}
    	}
    	return $res;
    }
    
    
    /**
     * 获得课程
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_course($params,$siteid,$school='')
    {
    	global $mconf;
    	$modules = get_moudule_info();
    	if(!isset($modules['college']))
    	{
    		return false;
    	}
    	$module_conf = $modules['college'];
    	$catlist = array();
    	
    	if(empty($siteid))
    	{
    		$this->db->prepare('select * from ' . DB_PRE . 'site_university where universityid=? status=?');
    		$this->db->set_string($school);
    		$this->db->set_int(STATUS_NORMAL);
    		$res = $this->db->execute();
    	}
    	else 
    	{
    		$this->db->prepare('select * from ' . DB_PRE . 'site_university where universityid=? and status=? and siteid=?');
    		$this->db->set_string($school);
    		$this->db->set_int(STATUS_NORMAL);
    		$this->db->set_string($siteid);
    		$res = $this->db->execute();
    	}
    	
    	foreach ($res as $v)
    	{
    		$catlist[] = array($v['id'], $v['siteid']);
    	}
    	$res = true;
    	if(is_array($catlist))
    	{
    		include_once 'content.class.php';
    		$c = new content('course');
    		include_once 'type.class.php';
    		$type_obj = new type();
    		$tmp_type_list = $type_obj->get_typs('edufe_course_type','edufe_course_type');
   			$type_list = array();
   			foreach ($tmp_type_list as $code => $v)
   			{
   				$type_list[$code] = $v['tname'];
   			}
   			
    		foreach ($catlist as $v)
    		{
    			foreach ($params as $p)
    			{
	    			//如果不是强制同步
		    		if($this->do_same == false)
		    		{
		    			$ck_res = $this->check_record_exists($p[0],'course',$v[1],$school);
		    			//如果该站点已经同步过该记录
		    			if($ck_res == true)
		    			{
		    				continue;
		    			}
		    		}
		    		$subject = array_search($p[1],$type_list);
		    		
		    		$data = array(
	    				'title'		=>	$p[3],
	    				'link'		=>	'',
	    				'listorder'	=>	0,
	    				'siteid'	=>	$v[1],
	    				'catid'		=>	$v[0],
	    				'status' 	=> 	STATUS_WAIT,
	    				'school'	=>	$school,
	    				'module'	=>	'course',
	    				'username'	=>	'admin',
	    				'stype1'	=>	$subject==false ? '' : $subject,
	    				'modelid'	=>	$module_conf['course_content_model'],
	    				'content'	=>	$p[8],
	    				'addtime'	=>	$p[6],
	    				'coursecode'=>	$p[2],
	    				'url1'		=>	$p[4],
	    				'url2'		=>	$p[5],
	    				'teacher'	=>	$p[7],
	    				'honor'		=>	$p[10],
	    				'stype2'	=>	$p[9]==1 ? 'jpck_1' : 'jpck_0',
	    			);
	    			
	    			$id = $c->add($data);
		            if(empty($id))
		            {
		                return false;
		            }
		            $this->make_record($p[0],$id,'course',$v[1],$school);
                    $relcat = array($v[0]);
 		            $c->add_rel($id, $relcat);
    			}
    		}
    	}
    	return true;
    }
    
    
    /**
     * faq信息
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_faq($params, $siteid, $school='')
    {
    	$catlist = array();
    	if(empty($siteid))
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="faq" and status='.STATUS_NORMAL);
    	}
    	else 
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="faq" and siteid="'.$siteid.'" and status='.STATUS_NORMAL);
    	}
    	if(is_array($res))
    	{
    		foreach ($res as $r)
    		{
    			$catlist[$r['catid']] = $r;
    		}
    	}
    	include_once 'content.class.php';
    	$c = new content;
    	foreach ($catlist as $catid=>$ca)
    	{
    		$relcat = array($catid);
    		foreach ($params as $p)
    		{
    			//如果不是强制同步
    			if($this->do_same == false)
    			{
    				$ck_res = $this->check_record_exists($p[0],'faq',$ca['siteid'],$school);
    				//如果该站点已经同步过该记录
    				if($ck_res == true)
    				{
    					continue;
    				}
    			}
    			$data = array(
    				'title'			=>	$p[1],
    				'thumb'			=>	'',
    				'link'			=>	'',
    				'listorder'		=>	0,
    				'siteid'		=>	$ca['siteid'],
    				'catid'			=>	$catid,
    				'status' 		=> 	STATUS_WAIT,
    				'school'		=>	$school,
    				'module'		=>	'category',
    				'username'		=>	'admin',
    				'modelid'		=>	$ca['modelid'],
    				'content'		=>	$p[3],
    				'description'	=>	$p[2],
    				'addtime'		=>	date('Y-m-d H:i:s', $p[4]),
    			);
    			$id = $c->add($data);
	            if(empty($id))
	            {
	                return false;
	            }
	            $this->make_record($p[0],$id,'faq',$ca['siteid'],$school);
	            $c->add_rel($id, $relcat);
    		}
    	}
    	return true;
    }
    
    
    /**
     * 新闻类信息
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_cat_content($params, $siteid, $catcode, $school='')
    {
    	$catlist = array();
    	if(empty($siteid))
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="'.$catcode.'" and status='.STATUS_NORMAL);
    	}
    	else 
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="'.$catcode.'" and siteid="'.$siteid.'" and status='.STATUS_NORMAL);
    	}
    	if(is_array($res))
    	{
    		foreach ($res as $r)
    		{
    			$catlist[$r['catid']] = $r;
    		}
    	}
    	include_once 'content.class.php';
    	$c = new content;
    	foreach ($catlist as $catid=>$ca)
    	{
    		$relcat = array($catid);
    		foreach ($params as $p)
    		{
    			$tmpdata = get_object_vars($p);
    			//如果不是强制同步
    			if($this->do_same == false)
    			{
    				$ck_res = $this->check_record_exists($tmpdata['contentid'],$catcode,$ca['siteid'],$school);
    				//如果该站点已经同步过该记录
    				if($ck_res == true)
    				{
    					continue;
    				}
    			}
    			$data = array(
    				'title'		=>	$tmpdata['title'],
    				'thumb'		=>	$tmpdata['thumb'],
    				'link'		=>	$tmpdata['islink']==1 ?  $tmpdata['url'] : '',
    				'listorder'	=>	$tmpdata['listorder'],
    				'siteid'	=>	$ca['siteid'],
    				'catid'		=>	$catid,
    				'status' 	=> 	STATUS_WAIT,
    				'school'	=>	$school,
    				'module'	=>	'category',
    				'username'	=>	'admin',
    				'modelid'	=>	$ca['modelid'],
    				'content'	=>	$tmpdata['content'],
    				'addtime'	=>	date('Y-m-d H:i:s', $tmpdata['inputtime']),
    			);
    			$id = $c->add($data);
	            if(empty($id))
	            {
	                return false;
	            }
	            $this->make_record($tmpdata['contentid'],$id,$catcode,$ca['siteid'],$school);
	            $c->add_rel($id, $relcat);
    		}
    	}
    	return true;
    }
    
    
    /**
     * 公告
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_announce($params, $siteid, $school='')
    {
    	$catlist = array();
    	if(empty($siteid))
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="announce" and status='.STATUS_NORMAL);
    	}
    	else 
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="announce" and siteid="'.$siteid.'" and status='.STATUS_NORMAL);
    	}
    	if(is_array($res))
    	{
    		foreach ($res as $r)
    		{
    			$catlist[$r['siteid']][$r['catid']] = $r;
    		}
    	}
    	include_once 'content.class.php';
    	$c = new content('category');
    	foreach ($catlist as $_siteid=>$site)
    	{
    		$site_obj = site_config($_siteid);
    		$site_conf = $site_obj->get_site_conf();
    		if(!isset($site_conf['ANNOUNCE_TYPE']))
    		{
    			continue;
    		}
    		$access_announce_type = explode(',', str_replace('ann_type_','',$site_conf['ANNOUNCE_TYPE']));
    		foreach ($site as $catid=>$ca)
    		{
	    		$relcat = array($catid);
	    		foreach ($params as $p)
	    		{
	    			if(!in_array($p[4], $access_announce_type))
	    			{
	    				continue;
	    			}
	    			if($p[2] < 0 || ($p[2] > 0 && $p[2] != $_siteid))
	    			{
	    				continue;
	    			}
	    			//如果不是强制同步
	    			if($this->do_same == false)
	    			{
	    				$ck_res = $this->check_record_exists($p[0],'announce',$_siteid,$school);
	    				//如果该站点已经同步过该记录
	    				if($ck_res == true)
	    				{
	    					continue;
	    				}
	    			}
	    			$data = array(
	    				'title'		=>	$p[1],
	    				'link'		=>	'',
	    				'listorder'	=>	$p[6],
	    				'siteid'	=>	$_siteid,
	    				'catid'		=>	$catid,
	    				'status' 	=> 	STATUS_WAIT,
	    				'school'	=>	$school,
	    				'module'	=>	'category',
	    				'username'	=>	'admin',
	    				'stype1'	=>	'ann_type_'.$p[4],
	    				'modelid'	=>	$ca['modelid'],
	    				'content'	=>	$p[3],
	    				'addtime'	=>	date('Y-m-d H:i:s', $p[5]),
	    			);
	    			$id = $c->add($data);
		            if(empty($id))
		            {
		                return false;
		            }
		            $this->make_record($p[0],$id,'announce',$ca['siteid'],$school);
		            $c->add_rel($id, $relcat);
	    		}
    		}
    	}
    	return true;
    }
    
    /**
     * 教师介绍
     *
     * @param array $params
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    public function add_teacher($params, $siteid, $school='')
    {
    	$catlist = array();
    	if(empty($siteid))
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="teacher" and status='.STATUS_NORMAL);
    	}
    	else 
    	{
    		$res = $this->db->query('select catid,siteid,modelid from '.DB_PRE.'category where catcode="teacher" and siteid="'.$siteid.'" and status='.STATUS_NORMAL);
    	}
    	if(is_array($res))
    	{
    		foreach ($res as $r)
    		{
    			$catlist[$r['siteid']][$r['catid']] = $r;
    		}
    	}
    	include_once 'content.class.php';
    	$c = new content('category');
    	foreach ($catlist as $_siteid=>$site)
    	{
    		foreach ($site as $catid=>$ca)
    		{
	    		$relcat = array($catid);
	    		foreach ($params as $p)
	    		{
	    			if($p[10] != $_siteid)
	    			{
	    				continue;
	    			}
	    			//如果不是强制同步
	    			if($this->do_same == false)
	    			{
	    				$ck_res = $this->check_record_exists($p[0],'teacher',$_siteid,$school);
	    				//如果该站点已经同步过该记录
	    				if($ck_res == true)
	    				{
	    					continue;
	    				}
	    			}
	    			$data = array(
	    				'title'		=>	$p[1],
	    				'link'		=>	'',
	    				'listorder'	=>	0,
	    				'siteid'	=>	$_siteid,
	    				'catid'		=>	$catid,
	    				'status' 	=> 	STATUS_WAIT,
	    				'school'	=>	$school,
	    				'module'	=>	'category',
	    				'username'	=>	'admin',
	    				'modelid'	=>	$ca['modelid'],
	    				'contact'	=>	$p[2],
	    				'duty'		=>	$p[3],
	    				'content'	=>	$p[4],
	    				'thumb'		=>	$p[5],
	    				'addtime'	=>	date('Y-m-d H:i:s'),
	    				'address'	=>	$p[7],
	    				'sex'		=>	$p[8],
	    				'work'		=>	$p[9],
	    				'birthday'	=>	$p[6]
	    			);
	    			$id = $c->add($data);
		            if(empty($id))
		            {
		                return false;
		            }
		            $this->make_record($p[0],$id,'teacher',$ca['siteid'],$school);
		            $c->add_rel($id, $relcat);
	    		}
    		}
    	}
    	return true;
    }
    
    /**
     * 记录同步信息的记录，防止重复
     *
     * @param string $webid
     * @param string $selfid
     * @param string $catcode
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    private function make_record($webid, $selfid, $catcode, $siteid, $school)
    {
    	$data = array(
    		'webid'	=>	$webid,
    		'selfid'	=>	$selfid,
    		'catcode'	=>	$catcode,
    		'siteid'	=>	$siteid,
    		'school'	=>	$school,
    		'addtime'	=>	time()
    	);
    	$res = $this->db->insert(DB_PRE . 'web_same_record', $data);
    	return $res;
    }
    
    /**
     * 检查信息是否已同步过
     *
     * @param string $webid
     * @param string $catcode
     * @param string $siteid
     * @param string $school
     * @return bool
     */
    private function check_record_exists($webid,$catcode,$siteid,$school)
    {
    	$this->db->prepare('select count(*) as nums from ' . DB_PRE . 'web_same_record where webid=? and catcode=? and siteid=? and school=?');
    	$this->db->set_string($webid);
    	$this->db->set_string($catcode);
    	$this->db->set_string($siteid);
    	$this->db->set_string($school);
    	$res = $this->db->execute(true);
    	if(isset($res['nums']) && intval($res['nums']) > 0)
    	{
    		return true;
    	}
    	return false;
    }
    
    function get_type_list($pcodes)
    {
    	$list = array();
    	$res = $this->db->query('select * from ' . DB_PRE . 'type where parentcode in ("'.implode('","', $pcodes).'") and status=99');
    	if(is_array($res))
    	{
    		foreach ($res as $v)
    		{
    			$list[$v['tcode']] = $v['tname'];
    		}
    	}
    	return $list;
    }
    
    function school_get_enroll($starttime,$endtime,$school)
    {
    	$stamp_start_time 	= date('Y-m-d H:i:s',$starttime);
    	$stamp_end_time		= date('Y-m-d H:i:s',$endtime);
    	$this->db->prepare('select * from ' . DB_PRE .'site_enroll where universityid=? and ((addtime>=? and addtime<=?) or (lasttime>=? and lasttime<=?)) and status=?');
    	$this->db->set_string($school);
    	$this->db->set_int($starttime);
    	$this->db->set_int($endtime);
    	$this->db->set_string($stamp_start_time);
    	$this->db->set_string($stamp_end_time);
    	$this->db->set_int(STATUS_NORMAL);
    	$res = $this->db->execute();
    	$typelist = $this->get_type_list(array('byzslx','zy','zzmm','minzu','zzzl'));
    	$list = array();
    	if(is_array($res))
    	{
    		foreach ($res as $k=>$v)
    		{
    			$tmp = $v;
    			$data = unserialize($v['data']);
    			$data['zzzl'] = !empty($data['zzzl']) ? (isset($typelist[$data['zzzl']]) ? $typelist[$data['zzzl']] : '未知分类') : '';
    			$data['minzu'] = !empty($data['minzu']) ? (isset($typelist[$data['minzu']]) ? $typelist[$data['minzu']] : '未知民族') : '';
    			$data['zzmm'] = !empty($data['zzmm']) ? (isset($typelist[$data['zzmm']]) ? $typelist[$data['zzmm']] : '未知政治面貌') : '';
    			$data['zy'] = !empty($data['zy']) ? (isset($typelist[$data['zy']]) ? $typelist[$data['zy']] : '未知职业') : '';
    			$data['byzslx'] = !empty($data['byzslx']) ? (isset($typelist[$data['byzslx']]) ? $typelist[$data['byzslx']] : '未知毕业证书类型') : '';
    			$tmp['data'] = serialize($data);
    			$list[] = $tmp;
    		}
    	}
    	return $list;
    }
    
    
    function school_get_complaint($starttime,$endtime,$school)
    {
    	$stamp_start_time 	= date('Y-m-d H:i:s',$starttime);
    	$stamp_end_time		= date('Y-m-d H:i:s',$endtime);
    	$this->db->prepare('select * from ' . DB_PRE .'complaint where ((addtime>=? and addtime<=?) or (lasttime>=? and lasttime<=?)) and status=?');
    	$this->db->set_int($starttime);
    	$this->db->set_int($endtime);
    	$this->db->set_string($stamp_start_time);
    	$this->db->set_string($stamp_end_time);
    	$this->db->set_int(STATUS_NORMAL);
    	$res = $this->db->execute();
    	return $res;
    }
}
?>
